package com.sunlight.boot.controller;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sunlight.boot.model.SysUser;
import com.sunlight.boot.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;

import javax.validation.Valid;
import javax.validation.groups.Default;
import javax.validation.constraints.NotBlank;

/**
 * 前端控制器
 *
 * @author ChangYongjin
 * @date 2021-05-20
 */
@Api(tags = "")
@RestController
@RequestMapping("/api/user")
public class SysUserController {

    @Autowired
    protected SysUserService sysUserService;

    /**
     * 根据id获取单条信息
     *
     * @return 实体
     */
    @ApiOperation(value = "信息", consumes = "application/x-www-form-urlencoded")
    @GetMapping("/info")
    public SysUser info(@NotBlank @ApiParam(value = "主键标识", required = true) String userId) {
        return sysUserService.getById(userId);
    }

    /**
     * 新增
     *
     * @param sysUser
     * @return
     */
    @ApiOperation(value = "新增", consumes = "application/x-www-form-urlencoded")
    @PutMapping("/add")
    public void add(@Validated({Default.class}) SysUser sysUser) {
        sysUserService.save(sysUser);
    }

    /**
     * 编辑
     *
     * @param sysUser
     */
    @ApiOperation(value = "编辑", consumes = "application/x-www-form-urlencoded")
    @PutMapping("/edit")
    public void edit(@Validated({Default.class}) SysUser sysUser) {
        sysUserService.saveOrUpdate(sysUser);
    }

    /**
     * 删除
     *
     * @param userId 主键标识
     * @return
     */
    @ApiOperation(value = "删除", consumes = "application/x-www-form-urlencoded")
    @DeleteMapping("/delete")
    public void delete(@NotBlank @ApiParam(value = "主键标识", required = true) String userId) {
        sysUserService.removeById(userId);
    }

    /**
     * 分页查询
     */
    @ApiOperation(value = "分页查询", consumes = "application/x-www-form-urlencoded")
    @PostMapping("/list")
    public Page<SysUser> list(@Validated Page pageQuery, @Validated({Default.class}) SysUser sysUser) {
        return sysUserService.page(pageQuery, Wrappers.query(sysUser));
    }

}

